package fun.codefarmer.pm.service.Impl;

import fun.codefarmer.pm.mapper.DepartMentMapper;
import fun.codefarmer.pm.model.DepartMent;
import fun.codefarmer.pm.model.RespBean;
import fun.codefarmer.pm.service.DepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ ClassName DepartmentServiceImpl
 * @ Descriotion TODO
 * @ author  codeFarmer_z
 * @ 公众号：码农教程
 * @ Date 2022/4/13 19:57
 **/
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartMentMapper departMentMapper;
    @Override
    public List<DepartMent> getAllDepartment() {
        List<DepartMent> list = departMentMapper.getAllDepartmentByParentId(-1);
        return list;
    }

    @Override
    public void addDep(DepartMent department) {
        department.setEnabled(true);
        departMentMapper.addDep(department);
    }

    @Override
    public RespBean deleteDep(Integer id) {
        //存储过程 返回的结果时存在实体类中的。所以需要创建个对象
        DepartMent department = new DepartMent();
        department.setId(id);
        departMentMapper.deleteDepById(department);
        if (department.getResult() == -2) {
            return RespBean.error("含有子部门删除失败！");
        } else if (department.getResult() == -1) {
            return RespBean.error("部门下含有员工删除失败！");
        } else if (department.getResult() == 1) {
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败，开发人员不知道什么原因！");
    }

    @Override
    public List<DepartMent> getAllDepartmentWithOutChildren() {
        List<DepartMent> list = departMentMapper.getAllDepartmentWithOutChildren();
        return list;
    }
}
